class_name RoundAutoFightSys
extends System

const CATCH_READ = ["FrameTime", "SkillAfterDelay", "SkillCmdC"]

var catch_read := []
#dirty:{fighter: number, cmd_arr: SkillCmdC[]}
var dirty = null


func _init():
	system_name = "RoundAutoFightSys"
	listen_modify("round_start", "RoundStateC", ["Ext", "SkillCmdC", "RoundStateC"])


func _system_ready():
	catch_read = world.query_manager.fetch_components(CATCH_READ)


func _system_process(_d):
	if not dirty:
		return
	var read = catch_read
	if read[0].data.count == read[1].data.value:
		var cmd = dirty.cmd_arr.pop_front()
		for skill_cmd in cmd.arr:
			read[2].get(dirty.fighter).value = skill_cmd
		if dirty.cmd_arr.size() == 0:
			dirty = null


#@listenModify(RoundStateC)
#@read(Single<Ext>, Multi<SkillCmdC>, Multi<RoundStateC>)
func round_start(e, read: Array):
	var id = e.id
	var round_state = read[2].get(id)
	if round_state.value != 1:
		return
	var cmd_arr = read[0].data.auto_skill(id)
	if cmd_arr && cmd_arr.size() > 0:
		var cmd = cmd_arr.pop_front()
		for skill_cmd in cmd.arr:
			read[1].get(id).value = skill_cmd
		if cmd_arr.size() > 0:
			dirty = {"fighter": id, "cmd_arr": cmd_arr}
